package com.lijian.community.controller;

import com.lijian.community.dto.NoticeDTO;
import com.lijian.community.dto.PaginationDTO;
import com.lijian.community.enums.NoticeTypeEnum;
import com.lijian.community.model.User;
import com.lijian.community.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/notification/{id}")
    public String profile(@PathVariable Integer id, HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/";
        }

        NoticeDTO noticeDTO = noticeService.read(id, user);
        if(NoticeTypeEnum.REPLY_COMMENT.getType() == noticeDTO.getType()
                || NoticeTypeEnum.REPLY_QUESTION.getType() == noticeDTO.getType()){
            return "redirect:/question/" + noticeDTO.getOuterId();
        }

        return "redirect:/";
    }
}
